<?php
declare(strict_types=1);
/*
箱号追踪表
*/
namespace Modules\Purchasing\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Purchasing\Http\Repositories\SupplierReget;
use Illuminate\Http\Request;
use Modules\Purchasing\Models\Supplier;
use Modules\Purchasing\Models\Supplierschedule;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Modules\Purchasing\Models\Purchaseorder;
use Modules\Purchasing\Models\Purchaseorderschedule;
use Modules\Purchasing\Models\Barcodeprinting;
use Modules\Purchasing\Models\Suppliernamedelivery;
use Illuminate\Support\Facades\DB;
use SimpleSoftwareIO\QrCode\Facades\QrCode;   
use Catch\Exceptions\FailedException;
use Modules\Purchasing\Models\Provisional;
use Modules\Purchasing\Models\Provisionalschedule;
use Modules\Warehouse\Models\Storagelocation;
use Modules\Purchasing\Models\Inspectionlisttable;
use Modules\Purchasing\Models\Purchasewarehousing;

class BarcodeController extends Controller
{
   
    public function __construct(
        protected readonly Barcodeprinting $model
    ){}

    /* 箱号装态-查询接口
     * @param $id
     * @return mixed
     */
    public function casenumber($code)
    {
        
        $kingDeeService = new warehouseService();
        $datauser = $kingDeeService->uerdata();
        $data = [];
        $status = '';
        $quantityreceived = '';
        $fname = '';
        $repositoryname = '';
        $putquantity = '';
        $creator_id = '';
        $created_at = '';
        $Barcodeprinting = Barcodeprinting::select('purchaseorderschedule_id','created_at','creator_id')->where('barcode',$code)->first();
        if(empty($Barcodeprinting)){
            return throw new FailedException('亲，没有该条采购箱号哦!');
        }
        $fields = ['fmaterialid','fmaterialname','fmaterialmodel'];
        $where = ['id'=>$Barcodeprinting['purchaseorderschedule_id']];
        $Purchaseorderschedule = Purchaseorderschedule::getData($fields,$where);
        // $Purchaseorderschedule = Purchaseorderschedule::select('fmaterialid','fmaterialname','fmaterialmodel')->where('id',$Barcodeprinting['purchaseorderschedule_id'])->first();

        $Provisionalschedule = Provisionalschedule::select('supplier_id','provisional_id','quantityreceived','storagelocation','inspectionlisttable_id','putquantity','purchasewarehousing_id','storagelocation_id')->where('barcode',$code)->first();
        if(empty($Provisionalschedule)){
                $status = '已做单(未暂收)';
                $creator_id = $Barcodeprinting['creator_id'];
                $created_at = $Barcodeprinting['created_at'];
        }else{

           if(empty($Provisionalschedule['purchasewarehousing_id'])&&empty($Provisionalschedule['storagelocation_id'])){

                $temporary = Supplier::select('temporary')->where('id',$Provisionalschedule['supplier_id'])->where('temporary',1)->first();
                $Provisional = Provisional::select('creator_id','created_at')->where('id',$Provisionalschedule['provisional_id'])->first();
                $creator_id = $Provisional['creator_id'];
                $created_at = $Provisional['created_at'];
                $Inspectionlisttable = Inspectionlisttable::select('created_at','state')->where('id',$Provisionalschedule['inspectionlisttable_id'])->where('state',1)->first();
                $putquantity = $Provisionalschedule['quantityreceived'];
                $storagelocation_id = $Provisionalschedule['storagelocation'];

                if(!empty($temporary)&&empty($Inspectionlisttable)){
                    $status = '已暂收';
                }
                
                if(!empty($temporary)&&!empty($Inspectionlisttable)){
                    $status = '已检验';
                }

           }else{
                
                $Purchasewarehousing = Purchasewarehousing::select('created_at','state','creator_id')->where('id',$Provisionalschedule['purchasewarehousing_id'])->first();
                if($Purchasewarehousing['state']==1){
                    $status = '已入库';
                }else{
                    $status = '已扫描入库(待审核)';
                }
                $storagelocation_id = $Provisionalschedule['storagelocation_id'];
                $creator_id = $Purchasewarehousing['creator_id'];
                $created_at = $Purchasewarehousing['created_at'];
                $putquantity = $Provisionalschedule['putquantity'];
           }
          
            $Storagelocation = Storagelocation::select(
                'stash.fnumber',
                'stash.fname',
                'stash.fuseorgid_number',
                'stash.fuseorgid_name',
                'owning_id',
                'storagelocation.warehousecode',
                'storagelocation.repositoryname',
                'storagelocation.id'
                )
                ->join('stash as stash', 'stash.id', '=', 'storagelocation.owning_id')
                ->where('storagelocation.id',$storagelocation_id)
                ->first();
                $fname = $Storagelocation['fname'];
                $repositoryname = $Storagelocation['repositoryname'];
        }
        $created_at = json_encode($created_at);
        $created_at = json_decode($created_at);
        $ind = strpos($created_at,'T');

        $data['status'] = $status;
        $data['barcode'] = $code;
        $data['putquantity'] = $putquantity;
        $data['created_at'] = rtrim(substr_replace($created_at,'',$ind+1),'T');
        $data['creator_id'] = isset($datauser[$creator_id])?$datauser[$creator_id]['username']:'';
        $data['fname'] = $fname;
        $data['repositoryname'] = $repositoryname;
        $data['fmaterialid'] = $Purchaseorderschedule['fmaterialid'];
        $data['fmaterialname'] = $Purchaseorderschedule['fmaterialname'];
        $data['fmaterialmodel'] = $Purchaseorderschedule['fmaterialmodel'];
        return $data;
        
      
    }


    /* 箱号-追踪
     * @param $id
     * @return mixed
     */
     public function codenumber($barcode,Request $request) 
     {
             

        $data = [];

        $Barcodeprinting = Barcodeprinting::select('purchaseorderschedule_id','created_at','creator_id','supplier_id')->where('barcode',$barcode)->first();
        if(empty($Barcodeprinting)){
            return $data;
        }
        $requestdata = $request->all();
        $requestdata['barcode'] = $barcode;
        $requestdata['purchaseorderschedule_id'] = $Barcodeprinting['Barcodeprinting'];
        $requestdata['supplier_id'] = $Barcodeprinting['supplier_id'];
        $requestdata['created_at'] = $Barcodeprinting['created_at'];
        $requestdata['creator_id'] = $Barcodeprinting['creator_id'];
        $kingDeeService = new warehouseService();
        $requestdata['datauser'] =  $kingDeeService->uerdata();
        
        
        $fields = ['purchaseorderschedule.fmaterialid','purchaseorderschedule.fmaterialname','purchaseorderschedule.fmaterialmodel','purchaseorder.fbillno','purchaseorder.fsupplierid_number','purchaseorder.fsupplierid_name'];
        $where[] = ['purchaseorderschedule.id'=>$requestdata['purchaseorderschedule_id']];
       
        $Purchaseorderschedule = Purchaseorderschedule::getDatajon($fields,$where);
        
        switch ($requestdata['codenumber'])
        {   
            //供应商送货
            case 'supplier':
                $resdata = $this->supplier($requestdata);
              
                
                break;  
            //暂收
            case 'provisionalreceipt':
                $resdata = $this->provisionalreceipt($requestdata);
              
                
                break;
            
            //检验
            case 'inspectionlist':
                $resdata = $this->inspectionlist($requestdata);
                
                break;

            //仓库id
            case 'purchasewarehousing':
                $resdata = $this->purchasewarehousing($requestdata);
                
                break;

            default:
        }
        if(empty($resdata['docno'])){
            return $data;   
        }
       
        $Storagelocation = Storagelocation::query()->with('wms')->where('id',$resdata['storagelocation'])->first();
        if(empty($Storagelocation)){
            $data['wms_code'] = '';//仓库
            $data['location'] = '';//库位
        }else{
            $data['wms_code'] = $Storagelocation['wms']['fname'];//仓库
            $data['location'] = $Storagelocation['repositoryname'];//库位
        }
        $created_at = json_encode($resdata['add_time']);
        $created_at = json_decode($created_at);
        $ind = strpos($created_at,'T');
     
        $data['lotnum'] = $requestdata['barcode'];//箱号
        $data['fbillno'] = $Purchaseorderschedule['fbillno'];//采购单号
        $data['material_no'] = $Purchaseorderschedule['fmaterialid'];//品号
        $data['fsupplierid_number'] = $Purchaseorderschedule['fsupplierid_number'];//供应商编码
        $data['fsupplierid_name'] = $Purchaseorderschedule['fsupplierid_name'];//供应商名称
        $data['docno'] = $resdata['docno'];//单据编号
        $data['qty'] = $resdata['qty'];//数量
        $data['creator'] = $resdata['creator'];//操作人
        $data['add_time'] = rtrim(substr_replace($created_at,'',$ind+1),'T');//操作时间
        $data['doc_type'] = $resdata['doc_type'];
        
        return $data;
         
     }

    /* 供应商箱号-追踪
     * @param $id
     * @return mixed
     */
    protected function supplier($requestdata) 
    {
       
        $data = [];
        $supplierOrder = Supplier::select('supplierOrder')->where('id',$requestdata['supplier_id'])->first();
        $where = ['supplier_id'=>$requestdata['supplier_id']];
        $Supplierschedule = Supplierschedule::getData(['quantity'],$where);

        if(!empty($Supplierschedule)){
            $data['docno'] = $supplierOrder['supplierOrder'];//单据编号
            $data['qty'] = $Supplierschedule['quantity'];//数量
            $data['creator'] = isset($requestdata['datauser'][$requestdata['creator_id']])?$requestdata['datauser'][$requestdata['creator_id']]['username']:'';//操作人
            $data['add_time'] = $requestdata['created_at'];//操作时间
            $data['storagelocation'] = '';
            $data['doc_type'] = '供应商送货';
        }else{
            $data['storagelocation'] = '';
            $data['docno'] = '';
            $data['qty'] = '';
            $data['creator'] = '';
            $data['add_time'] = 'created_at';
            $data['doc_type'] = '';
        }
      return $data;


    }

    /* 暂收箱号-追踪
     * @param $id
     * @return mixed
     */
    protected function provisionalreceipt($requestdata) 
    {
        $data = [];
        
        $where = ['provisionalschedule.barcode'=>$requestdata['barcode']];
        $fields = ['provisional.ordernumber','provisional.creator_id','provisional.created_at','provisionalschedule.quantityreceived','provisionalschedule.storagelocation'];
        $Provisionalschedule = Provisionalschedule::getDatajon($fields,$where);
       
        if(!empty($Provisionalschedule)){
            
            $data['docno'] = $Provisionalschedule['ordernumber'];//单据编号
            $data['qty'] = $Provisionalschedule['quantityreceived'];//数量
            $data['storagelocation'] = $Provisionalschedule['storagelocation'];//库位id
            $data['creator'] = isset($requestdata['datauser'][$Provisionalschedule['creator_id']])?$requestdata['datauser'][$Provisionalschedule['creator_id']]['username']:'';//操作人
            $data['add_time'] = $Provisionalschedule['created_at'];//操作时间
             $data['doc_type'] = '暂收';
        }else{
          
            $data['storagelocation'] = '';
            $data['docno'] = '';
            $data['qty'] = '';
            $data['creator'] = '';
            $data['add_time'] = 'created_at';
            $data['doc_type'] = '';
        }

    return $data;
        
    }

    /*检验箱号-追踪
    * @param $id
    * @return mixed
    */
    protected function inspectionlist($requestdata) 
    {
        $data = [];
        $where = [];
        $where = ['provisionalschedule.barcode'=>$requestdata['barcode']];
        // $where[] = ['inspectionlisttable.state'=>1];
        $fields = ['inspectionlisttable.inspectionnumber','provisionalschedule.creator_id','inspectionlisttable.updated_at','provisionalschedule.quantityreceived','provisionalschedule.storagelocation'];
        $Provisionalschedule = Provisionalschedule::getDatajoninspection($fields,$where);
       
       
        if(!empty($Provisionalschedule)){
            $data['doc_type'] = '检验';
            $data['docno'] = $Provisionalschedule['inspectionnumber'];//单据编号
            $data['qty'] = $Provisionalschedule['quantityreceived'];//数量
            $data['storagelocation'] = $Provisionalschedule['storagelocation'];//库位id
            $data['creator'] = isset($requestdata['datauser'][$Provisionalschedule['creator_id']])?$requestdata['datauser'][$Provisionalschedule['creator_id']]['username']:'';//操作人
            $data['add_time'] = $Provisionalschedule['updated_at'];//操作时间
        }else{
             $data['storagelocation'] = '';
            $data['docno'] = '';
            $data['qty'] = '';
            $data['creator'] = '';
            $data['add_time'] = 'created_at';
            $data['doc_type'] = '';
        }
       return $data;
           
      
        
    }

    
    /*入库箱号-追踪
    * @param $id
    * @return mixed
    */
    protected function purchasewarehousing($requestdata) 
    {
        $data = [];
        $where = ['provisionalschedule.barcode'=>$requestdata['barcode']];
        $fields = ['purchasewarehousing.warehosnumber','purchasewarehousing.creator_id','purchasewarehousing.created_at','provisionalschedule.putquantity','provisionalschedule.storagelocation_id'];
        $Provisionalschedule = Provisionalschedule::getDatajoninpurchasewa($fields,$where);
        if(!empty($Provisionalschedule)){
            $data['storagelocation'] = $Provisionalschedule['storagelocation_id'];//库位id
            $data['docno'] = $Provisionalschedule['warehosnumber'];//单据编号
            $data['qty'] = $Provisionalschedule['putquantity'];//数量
            $data['creator'] = isset($requestdata['datauser'][$Provisionalschedule['creator_id']])?$requestdata['datauser'][$Provisionalschedule['creator_id']]['username']:'';//操作人
            $data['add_time'] = $Provisionalschedule['created_at'];//操作时间
            $data['doc_type'] = '采购入库';
        }else{
            $data['storagelocation'] = '';//库位id
            $data['docno'] = '';
            $data['qty'] = '';
            $data['creator'] = '';
            $data['add_time'] = '';
              $data['doc_type'] = '';
        }
        return $data;
       
    }


 




}
